#include "Observer.h"
#include "Logger.h"

namespace BPL
{
Observer::~Observer()
{
    delAllObs();
}

void Observer::delAllObs()
{
    iterator itb = mpt_list.begin();
    const_iterator ite = mpt_list.end();

    for(; itb!=ite; )
    {
        mpt_list.erase(itb++);
    }
}

void Observer::addObs(Observable* obs)
{
    mpt_list.push_back(obs);
}

void Observer::delObs(Observable* obs)
{
    iterator it= std::find(mpt_list.begin(),mpt_list.end(),obs);

    if(it != mpt_list.end())
	{
		(*it)->delObs(this);
        mpt_list.erase(it);
	}
}

void Observer::notify(Observable* obs)
{

}

Observable::~Observable()
{
   delAllObs();
}

void Observable::delAllObs()
{
    iterator itb = mpt_list.begin();
    const_iterator ite = mpt_list.end();

    for(; itb!=ite; )
    {
        mpt_list.erase(itb++);

    }
}

void Observable::addObs(Observer* obs)
{
    mpt_list.push_back(obs);

    obs->addObs(this);
}

void Observable::delObs(Observer* obs)
{
    iterator it= find(mpt_list.begin(),mpt_list.end(),obs);
    if(it != mpt_list.end())
        mpt_list.erase(it);
}

void Observable::_notify()
{
    iterator itb = mpt_list.begin();
    const_iterator ite = mpt_list.end();

    for(; itb!=ite;)
    {
        if((*itb)!=NULL)
        {
            (*itb)->notify(this);
            itb++;
        }
        else
            mpt_list.erase(itb++);
    }
}

}
